package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.CacheConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.UserThreadLocal;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    private final WmSensitiveMapper sensitiveMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private final static String SENSITIVE = "SENSITIVE";

    @Override
    @CacheEvict(cacheNames = CacheConstants.SENSITIVE_PREFIX, allEntries = true)
    public ResponseResult saveSensitive(WmSensitive sensitive) {
        ValueOperations<String, String> operation = stringRedisTemplate.opsForValue();
//获取当前用户ID
        Integer userId = UserThreadLocal.get();
//幂等性
        if (!operation.setIfAbsent(SENSITIVE + userId, "1", 1, TimeUnit.SECONDS)) {
            throw new CustomException(AppHttpCodeEnum.NOT_REPETITION_OPERATION);
        }
        sensitive.setCreatedTime(new Date());

        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery()
                .eq(sensitive.getSensitives() != null, WmSensitive::getSensitives, sensitive.getSensitives());
        WmSensitive wmSensitive = sensitiveMapper.selectOne(wrapper);
        if (wmSensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词已存在，请重新命名！");
        }

        return ResponseResult.okResult(sensitiveMapper.insert(sensitive));
    }


    @Override
    @Cacheable(cacheNames = CacheConstants.SENSITIVE_PREFIX, key = "#dto.getPage()+#dto.getName()")
    public PageResponseResult sensitiveList(SensitiveDto dto) {
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> wrapper = Wrappers.<WmSensitive>lambdaQuery()
                .like(WmSensitive::getSensitives, dto.getName());

        Page pageList = sensitiveMapper.selectPage(page, wrapper);
        PageResponseResult responseResult = new PageResponseResult((int) pageList.getCurrent(),
                (int) pageList.getSize(), (int) pageList.getTotal());
        responseResult.setData(pageList.getRecords());
        return responseResult;
    }


}
